package com.archie.zhufengfm.utils;

/**
 * Created by Archie on 2015/10/20.
 */

import com.archie.zhufengfm.Constants;
import com.archie.zhufengfm.entity.AlbumDetails;
import com.archie.zhufengfm.entity.DiscoverCategory;
import com.archie.zhufengfm.entity.discoverrecommend.DiscoverRecommendAlbums;
import com.archie.zhufengfm.entity.discoverrecommend.DiscoverRecommendColumns;
import com.archie.zhufengfm.entity.discoverrecommend.DiscoverRecommendItem;
import com.archie.zhufengfm.entity.discoverrecommend.DiscoverRecommendSpecial;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

/**
 * 实体类的parse解析工具类
 */
public class EntityParseUtil {

    private EntityParseUtil(){}

    public static AlbumDetails parseAlbumDetails(JSONObject json){
        AlbumDetails ret = null;

        if (json != null) {

            try {

                int code = json.getInt("ret");

                if (code == Constants.TASK_RESULT_OK) {

                    ret = new AlbumDetails();

                    ret.parseJSON(json);

                }


            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        return ret;
    }



    /**
     * 解析发现分类的数据
     * @param json
     * @return
     */
    public static List<DiscoverCategory> parseDiscoverCategory(JSONObject json){
        List<DiscoverCategory> ret = null;

        if (json != null) {

            try {

                int code = json.getInt("ret");

                if(code == Constants.TASK_RESULT_OK){

                    ret = new LinkedList<DiscoverCategory>();

                    JSONArray array = json.getJSONArray("list");

                    int len = array.length();

                    if(len > 0){

                        for (int i = 0; i < len; i++) {

                            JSONObject jsonObject = array.getJSONObject(i);

                            DiscoverCategory category = new DiscoverCategory();

                            // 利用实体类内部实现 JSON 解析，外部的代码就是调用方法即可
                            // 代码简洁，数据处理交给实体类自己处理
                            category.parseJSON(jsonObject);

                            ret.add(category);

                        }

                    }

                }


            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        return ret;
    }

    /**
     * 解析发现模块中推荐栏目中的数据结构
     * @param jsonObject
     * @return
     */
    public static List<DiscoverRecommendItem> parseDiscoverRecommends(JSONObject jsonObject) {

        List<DiscoverRecommendItem> ret =null;

        if (jsonObject != null) {
            try {

                int code = jsonObject.getInt("ret");

                if (code == Constants.TASK_RESULT_OK) {

                    ret = new LinkedList<>();

                    JSONObject object =
                            jsonObject.getJSONObject("editorRecommendAlbums");

                    DiscoverRecommendAlbums editorRecommend =
                            new DiscoverRecommendAlbums();

                    editorRecommend.parseJSON(object);

                    ret.add(editorRecommend);

                    // --------------------------------------------------------
                    // TODO 解析精品听单
                    JSONObject specialColumnJSON = jsonObject.getJSONObject("specialColumn");

                    DiscoverRecommendSpecial special =
                            new DiscoverRecommendSpecial();

                    special.parseJSON(specialColumnJSON);

                    ret.add(special);

                    // --------------------------------------------------------
                    // TODO 解析发现新奇

                    JSONObject columnRecommendsJSON = jsonObject.getJSONObject("discoveryColumns");

                    DiscoverRecommendColumns columns =
                            new DiscoverRecommendColumns();

                    columns.parseJSON(columnRecommendsJSON);

                    ret.add(columns);

                    // --------------------------------------------------------

                    // 解析热门推荐内部的内容

                    JSONObject hotRecommendsJSON = jsonObject.getJSONObject("hotRecommends");

                    JSONArray hotList = hotRecommendsJSON.getJSONArray("list");

                    int len = hotList.length();

                    for (int i = 0; i < len; i++) {

                        // 获取热门推荐内部list中的每一个推荐信息
                        JSONObject discoverRecommendAlbumJSON =
                                hotList.getJSONObject(i);

                        DiscoverRecommendAlbums albums =
                                new DiscoverRecommendAlbums();

                        albums.parseJSON(discoverRecommendAlbumJSON);

                        ret.add(albums);

                    }

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return ret;
    }
}
